﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UCS.XNA.Utils.Animation
{
    public class ScaleAnimation
    {
        public ScaleAnimation(IScalable target, float finalScale, float speed)
        {
            _target = target;
            _finalScale = finalScale;
            _speed = speed;
        }

        private IScalable _target;
        public IScalable Target
        {
            get { return _target; }
        }

        private float _finalScale;
        public float FinalScale
        {
            get { return _finalScale; }
        }

        private float _speed;
        public float Speed
        {
            get { return _speed; }
        }

        private bool _isFinished;
        public bool IsFinished
        {
            get { return _isFinished; }
        }

        #region Events

        public event EventHandler<EventArgs> AnimationFinished;

        #endregion

        #region Public Methods

        public void Update(GameTime gameTime)
        {
            if (_target.Scale != _finalScale)
            {
                if (System.Math.Abs(_finalScale - Target.Scale) < _speed)
                    _target.Scale = _finalScale;
                else
                {
                    var sign = System.Math.Sign(_finalScale - _target.Scale);
                    _target.Scale += sign * _speed;
                }
            }
            else if (!_isFinished)
            {
                _isFinished = true;
                this.RaiseAnimationFinishedEvent();
            }
        }

        #endregion

        #region Protected Methods

        private void RaiseAnimationFinishedEvent()
        {
            if (this.AnimationFinished != null)
                this.AnimationFinished(this, new EventArgs());
        }

        #endregion
    }
}
